#!/bin/bash
# dbench installer
###SHELLPACK preamble dbench-install 65b19870
GIT_LOCATION=git://git.samba.org/sahlberg/dbench.git
WEB_LOCATION=http://samba.org/ftp/tridge/dbench/
MIRROR_LOCATION="$WEBROOT/dbench/"

install-depends popt-devel zlib-devel
install-depends rpcgen
install-depends libtirpc-devel

###SHELLPACK parseargBegin
###SHELLPACK parseargEnd


###SHELLPACK git_fetch dbench-${VERSION}.tar.gz dbench-${VERSION}

# Build
###SHELLPACK self_extract 0001-Allow-reporting-of-workload-execution-times.patch
patch -p1 <$SHELLPACK_TEMP/0001-Allow-reporting-of-workload-execution-times.patch

###SHELLPACK self_extract 0002-Defer-reporting-of-execution-times.patch
patch -p1 <$SHELLPACK_TEMP/0002-Defer-reporting-of-execution-times.patch

###SHELLPACK self_extract 0003-Include-stdint_h.patch
patch -p1 <$SHELLPACK_TEMP/0003-Include-stdint_h.patch

###SHELLPACK self_extract 0004-Check-if-parent-is-alive-once-per-loadfile.patch
patch -p1 <$SHELLPACK_TEMP/0004-Check-if-parent-is-alive-once-per-loadfile.patch

###SHELLPACK build_start dbench-${VERSION}
###SHELLPACK build_autogen dbench-${VERSION}
###SHELLPACK build_configure dbench-${VERSION}

# Adapt for libtirpc
if [ -e /usr/lib64/libtirpc.so -o -e /usr/lib/libtirpc.so -o -e /usr/lib/x86_64-linux-gnu/libtirpc.so ]; then
	sed -i -e 's/^LIBS=.*/& -ltirpc/' Makefile

	if [ -d /usr/include/tirpc ]; then
		sed -i -e 's@gcc -g@gcc -g -I/usr/include/tirpc@' Makefile
	fi
fi

###SHELLPACK make_make_install
make tbench_srv
cp tbench_srv $SHELLPACK_SOURCES/dbench-${VERSION}-installed/bin
ln -s $SHELLPACK_SOURCES/dbench-${VERSION}-installed/bin/dbench $SHELLPACK_SOURCES/dbench-${VERSION}-installed/bin/tbench

# Fixups
if [ ! -e $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/client.txt ]; then
	cp $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/doc/dbench/loadfiles/client.txt $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share
fi

# Extract our loadfiles from the original one
head -n 600 $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/client.txt >$SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/client-warmup.txt
head -n 4173 $SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/client.txt | tail -n 3573 >$SHELLPACK_SOURCES/dbench-${VERSION}-installed/share/client-tiny.txt

echo dbench installed successfully
exit $SHELLPACK_SUCCESS

###SHELLPACK include_file 0001-Allow-reporting-of-workload-execution-times.patch
###SHELLPACK include_file 0002-Defer-reporting-of-execution-times.patch
###SHELLPACK include_file 0003-Include-stdint_h.patch
###SHELLPACK include_file 0004-Check-if-parent-is-alive-once-per-loadfile.patch
###SHELLPACK include_file 0005-Check-if-parent-is-alive-once-per-loadfile.patch
